package com.example.myapplication.found.file;

import com.example.myapplication.found.bean.FoundDayPointBean;
import com.example.myapplication.found.bean.StockBean;
import com.example.myapplication.found.bean.StockNameBean;
import com.example.myapplication.found.model.Fenxi0;
import com.example.myapplication.shortvideo.utils.GsonUtil;
import com.google.gson.reflect.TypeToken;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author: rongfeng
 * @date: 2022/2/10
 * @description
 */
public class FoundDayJsonFile {
    public void save(List<FoundDayPointBean> list, String fundCode) {
        String json = GsonUtil.toJson(list);
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date(System.currentTimeMillis());

            File file = new File("D:\\test\\" + fundCode + formatter.format(date) + ".json");

            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(json);
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public List<FoundDayPointBean> getSave(String fundCode) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        File file = new File("D:\\test\\" + fundCode + formatter.format(date) + ".json");
        try {
            if (file.exists()) {
                String str;
                FileInputStream fileInputStream = new FileInputStream(file);
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

                StringBuffer sb = new StringBuffer();
                String text = null;
                while ((text = bufferedReader.readLine()) != null) {
                    sb.append(text);
                }
                str = sb.toString();

                fileInputStream.close();
                inputStreamReader.close();
                bufferedReader.close();

                List<FoundDayPointBean> bean = GsonUtil.parse(str, new TypeToken<List<FoundDayPointBean>>() {
                }.getType());

                return bean;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void saveStock(String fundCode, StockBean list, int type) {
        String json = GsonUtil.toJson(list);
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date(System.currentTimeMillis());

            File file1 = new File("D:\\wrf\\stock");
            if (!file1.exists()) {
                file1.mkdirs();
            }

            File file = new File("D:\\wrf\\stock\\" + fundCode + "_" + formatter.format(date) + "_" + type + ".json");


            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(json);
            bw.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void saveStockName(String fundCode, List<StockNameBean> stockBean) {
        String json = GsonUtil.toJson(stockBean);
        try {
            File file1 = new File("D:\\wrf\\stockName");
            if (!file1.exists()) {
                file1.mkdirs();
            }

            File file = new File("D:\\wrf\\stockName\\" + fundCode + ".json");

            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(json);
            bw.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //    long currentTime = 1645780168624L;
    long currentTime = System.currentTimeMillis();

    public StockBean getStock(String fundCode, int type) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String date = formatter.format(new Date(currentTime));
//        date = "2022-08-25";
        File file = new File("D:\\wrf\\stock\\" + fundCode + "_" + date + "_" + type + ".json");
        try {
            if (file.exists()) {
                String str;
                FileInputStream fileInputStream = new FileInputStream(file);
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

                StringBuffer sb = new StringBuffer();
                String text = null;
                while ((text = bufferedReader.readLine()) != null) {
                    sb.append(text);
                }
                str = sb.toString();

                fileInputStream.close();
                inputStreamReader.close();
                bufferedReader.close();

                StockBean stockBean = GsonUtil.parse(str, StockBean.class);

                return stockBean;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<StockNameBean> getStockName(String fundCode) {
        File file = new File("D:\\wrf\\stockName\\" + fundCode + ".json");
        try {
            if (file.exists()) {
                String str;
                FileInputStream fileInputStream = new FileInputStream(file);
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                StringBuffer sb = new StringBuffer();
                String text = null;
                while ((text = bufferedReader.readLine()) != null) {
                    sb.append(text);
                }
                str = sb.toString();

                fileInputStream.close();
                inputStreamReader.close();
                bufferedReader.close();
                List<StockNameBean> stockBean = GsonUtil.parseList(str, new TypeToken<List<StockNameBean>>() {
                }.getType());
                return stockBean;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void saveRang(List<Fenxi0.RangResult> rangResults) {
        try {
            File file = new File("D:\\wrf\\stockTest\\RangResult.json");
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
            // 遍历写入
            BufferedWriter bw = new BufferedWriter(new FileWriter(file));
            for (Fenxi0.RangResult lineL : rangResults) {
                String json = GsonUtil.toJson(lineL);
                bw.write(json + ",");
            }
            bw.flush();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
